﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace drawDong
{
    public partial class Form5 : Form
    {
        private int mainColumns = 13;

        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            setSaveBoxName();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form6 frm = new Form6();
            frm.Show();
        }

        private void setSaveBoxName()
        {
            DirectoryInfo rootDir = new DirectoryInfo(GlobalVariables.fliterPath);
            FileInfo[] allDirs = rootDir.GetFiles();

            int si = allDirs.Length;
            int ct = si % mainColumns > 0 ? si / mainColumns + 1 : si / mainColumns;
            for (int i = 0; i < ct; i++)
            {
                this.dataGridView1.Rows.Add("", "", "", "", "", "", "");
            }

            int rows = this.dataGridView1.Rows.Count;
            int columns = mainColumns;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    string text = this.dataGridView1[j, i].Value == null ? "" : this.dataGridView1[j, i].Value.ToString();
                    Console.WriteLine(text);

                    int index = (rows - 1) * mainColumns + j;
                    if (index >= allDirs.Length)
                    {
                        break;
                    }
                    DataGridViewButtonCell xx = new DataGridViewButtonCell();
                    this.dataGridView1[j, i] = xx;
                    this.dataGridView1[j, i].Value = allDirs[index].Name.Replace(".txt", "");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedCellCollection dgscc = this.dataGridView1.SelectedCells;
            if (dgscc.Count <= 0)
            {
                MessageBox.Show("请选择删除的滤镜", "提示");
                return;
            }
            DataGridViewButtonCell dgvs = (DataGridViewButtonCell)dgscc[0];

            DialogResult dr = MessageBox.Show("确认删除滤镜("+ dgvs.Value + ")吗", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            FileInfo file = new FileInfo(GlobalVariables.fliterPath + @"\"  + dgvs.Value + ".txt");
            file.Delete();
            while (this.dataGridView1.Rows.Count > 0)
            {
                this.dataGridView1.Rows.RemoveAt(0);
            }
            setSaveBoxName();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Console.Write("1111111111111111");
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (null == this.dataGridView1[e.ColumnIndex, e.RowIndex].Value)
            {
                return;
            }
            string name = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            if (name.Length <= 0)
            {
                return;
            }
            Form9 frm = new Form9(name);
            frm.Show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            string[] ar = this.contextMenuStrip1.Tag.ToString().Split(';');
            if (ar.Length > 1) {
                int ColumnIndex = Convert.ToInt16(ar[0]);
                int RowIndex = Convert.ToInt16(ar[1]);

                string path = this.dataGridView1[ColumnIndex, RowIndex].Value.ToString();

                Clipboard.SetDataObject(path);

                MessageBox.Show("已复制：" + path);
            }
            Console.Write("3333333");
        }

        private void dataGridView1_CellContextMenuStripChanged(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
        {

        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                return;
            }

            for (int i = 0; i < this.dataGridView1.RowCount; i++)
            {
                for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
                {
                    this.dataGridView1[j, i].Selected = j == e.ColumnIndex && i == e.RowIndex;
                }
            }

            object obj = this.dataGridView1[e.ColumnIndex, e.RowIndex];
            int w2 = this.splitContainer1.Panel1.Width;
            int h2 = 0;
            for (int i = 0; i < e.ColumnIndex; i++)
            {
                w2 += this.dataGridView1[i, e.RowIndex].Size.Width;
            }

            for (int i = 0; i < e.RowIndex; i++)
            {
                h2 += this.dataGridView1[e.ColumnIndex, i].Size.Height;
            }

            Point p = new Point(e.Location.X + this.splitContainer1.SplitterWidth, e.Location.Y);
            p.X += w2;
            p.Y += h2;

            this.contextMenuStrip1.Tag = e.ColumnIndex + ";" + e.RowIndex;

            this.contextMenuStrip1.Show(PointToScreen(p));
        }
    }
}
